﻿@using RBS.Web.Controllers
@model RBS.Core.Model.Hotel.H_Hotels
@{
    ViewBag.Title = "Detail";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var hId = Model.H_id;
    var uid = -1;
    if (Request.IsAuthenticated)
    {
        uid = Convert.ToInt32(Context.User.Identity.Name);
    } 
} 
<div id="content"> 
    <div class="h1a">本地商户   @Html.Hidden("orderid",0,new{ value=@ViewBag.orderid})</div>
     @{
        @Html.Partial("_HotelHead",Model);
    }
    <div id="mainContent">
        <dl class="dtlcateg">
            <dd class="active"><a href="javascript:;">菜单</a></dd>
            <dd  ><a href="@Url.Action("HotelImg",new{ id=Model.H_id})">餐厅场景</a></dd>
            <dd><a href="@Url.Action("Comment", new { id = Model.H_id })">网友点评</a></dd>
        </dl>
        <dl id="CategoryUL" class="dtlcategdtl">
            <dd class="active" id="0">
                <a href="javascript:;">全部</a>
            </dd>
        </dl> 
        <dl class="dtlorderby">
            <dd class="active" ><a href="javascript:;">默认</a></dd>
            <dd><a href="javascript:;">价格</a></dd>
            <dd><a href="javascript:;">热度</a></dd>
            <dd><a href="javascript:;">评价数</a></dd>
            <dd><a href="javascript:;">好评率</a></dd>
        </dl>
        <div class="maindishlist">
            <ol id="CookbookUL">
            </ol>
        </div> 
    </div>
    <div id="sideContent">
        <div class="mymenu">
            <h2>我的菜单</h2>
            <div class="menulist">
                <ul id="orderUL">
                    <li>您还没有点菜</li>
                </ul>  
                <div class="total-warp">
                  @*  <a href="javascript:void(0);" class="J_DeletCartData"  onclick="DeleteOrderTemp(-1,2)">清空</a>
                    <dl class="price">
                        <dd class="item-count">合计数量：<b id="numcount"></b>份</dd>
                        <dd class="item-tit">合计金额：<b>¥</b> <strong id="priceCount"></strong></dd>
                        <dd class="item-label">节省：<b>¥</b> <strong>20</strong></dd>
                        <dd class="price-other">菜单金额以实际消费为准</dd>
                    </dl>*@
                    <a class="next-btn" href="#" id="BtnNextOrder">预定</a>
                </div>
            </div>
        </div>
    </div>
</div>
<script> 
        
                       
     //获取菜谱分类
    function GetCategory(hid) {
        var url = "@Url.Action("GetCategory")?hid=" + hid; 
        $.ajax({
            url:url,
            type: 'Post',
            dataType: 'json',
            success: function (result) { 
                if (result == null)
                    return; 
                $.each(result.Data, function (index, row) { 
                    var template = $('#CategoryTemp').html(); //获取模版的html
                    var bookData = Mustache.render(template, row);//把每行的数据填充到模版得到html内容
                    $("#CategoryUL").append(bookData);
                });
                $("#CategoryUL dd").each(function () { 
                    $(this).click(function () {
                        $("#CategoryUL dd").removeClass("active");
                        $(this).addClass("active");
                        //alert($(this).attr("id")); 
                        $("#CookbookUL").empty();
                        GetCookbook(hid, $(this).attr("id")); 
                    }); 
                });  
            }
        }); 
    }
    //显示菜单
    function GetCookbook(hid, cid) {
        var url = "@Url.Action("GetCookbook")?type=Cookbook&hid=" + hid + "&cid=" + cid; 
        $.ajax({
            url: url,
            type: 'Post',
            dataType: 'json',
            success: function (result) {
                if (result.Data.length==0) { 
                    var li = document.createElement('li');
                    var msg = document.createElement('div');
                    $(msg).attr("style", "float:right;width:60%");
                    $(msg).html("本类暂无菜单");
                    $(li).append(msg);
                    document.getElementById('CookbookUL').appendChild(li);
                    return;
                }   
                result.Data.sort(function(a,b){
                    return a["Price"]>b["Price"]?1:a["Price"]==b["Price"]?0:-1
                }); 
                $.each(result.Data, function (index, row) { 
                    var template = $('#CookbookTemp').html(); //获取模版的html
                    var bookData = Mustache.render(template, row);//把每行的数据填充到模版得到html内容
                    $("#CookbookUL").append(bookData);
                });
                $("#CookbookUL p").each(function () { 
                    $(this).click(function () {  
                        if (@uid==-1) { 
                            var url = "@Url.Action("Index", "Login", new { redirectUrl = Request.Url })";
                            window.location = url;
                        }
                        else if ($("#orderid").val() == "0") {
                            Order();
                        }
                        else {
                            AddOrder($(this).attr('Id'), $(this).attr('Name'), $(this).attr('Price'), 1, $("#orderid").val());
                        }

                    }); 
                });   
            }
        });
    }  
    function Order() {
        $.dialog({
            id: 'qx', lock: true,title: '填写订单', width: '710px', height: '520px', content:"url:@Url.Action("Order")?hotelid="+@Model.H_id+"&orderid="+$("#orderid").val() /*, ok: function () {
                                orderid = 123;
                                this.reload();
                                return false;
                                    }   */
        }); 
        
        /*if($("#orderid").val()!="0"){
            $("#aAddOrder").text("修改预定");}*/
    }

    //添加到菜谱
    function AddOrder(a, b, c,d,e) { 
        //alert(a + "," + b + "," + c + "," + d + "," + e);
        var tempurl = "@Url.Action("OrderTempDetail", "Order", new { Area = "User" })";
        $.ajax({
            url: tempurl,
            cache: false,
            type: 'POST',
            dataType: "json", 
            data: {
                CName: b,
                Price: c,
                HCId: a,
                Num: d,
                IsGuest: 0,
                UId:@uid,
                orderid:$("#orderid").val(),
                hid:@Model.H_id,
                hname:'@Model.H_Name'
            },
            success: function (result) { 
             
                if (result.Data == null)
                    return;  
                $("#orderUL").empty();
                var numCount = 0;
                var priceCount = 0; 
                $.each(result.Data, function (index, row) {
                    priceCount += row.Price*row.Num;
                    numCount += row.Num;
                
                    var template = $('#OrderTemp').html(); //获取模版的html
                    var bookData = Mustache.render(template, row);//把每行的数据填充到模版得到html内容
                    $("#orderUL").append(bookData);
                }); 
                $("#priceCount").html(priceCount);
                $("#numcount").html(numCount); 
            }
        });
    }
    //零时订单
    function GetOrderTempDetail(uid,oid) {
        var url = "@Url.Action("GetOrderTempDetail", "Order", new { Area = "User" })?uid=" + uid+"&oid="+oid; 
        $.ajax({
            url:url,
            type: 'Post',
            dataType: 'json',
            success: function (result) { 
                
                $("#orderUL").html("");
                if (result.Data == null)
                    return;

                var numCount = 0;
                var priceCount = 0;
                $.each(result.Data, function (index, row) { 
                    priceCount += row.Price*row.Num;
                    numCount += row.Num; 
                    var template = $('#OrderTemp').html(); //获取模版的html
                    var bookData = Mustache.render(template, row);//把每行的数据填充到模版得到html内容
                    $("#orderUL").append(bookData);
                }); 
                $("#priceCount").html(priceCount);
                $("#numcount").html(numCount);
             
            }
        }); 
    } 
    function DeleteOrderTemp(a, b) {  
        var tempurl = "@Url.Action("DeleteOrderTempDetail", "Order", new { Area = "User" })";
        $.ajax({
            url: tempurl,
            cache: false,
            type: 'POST',
            dataType: "json", 
            data: {
                id:a,
                type:b 
            },
            success: function (result) {  
                $("#orderUL").html("");
                if (result == null)
                    return;

                var numCount = 0;
                var priceCount = 0;
                $.each(result.Data, function (index, row) { 
                    priceCount += row.Price*row.Num;
                    numCount += row.Num; 
                    var template = $('#OrderTemp').html(); //获取模版的html
                    var bookData = Mustache.render(template, row);//把每行的数据填充到模版得到html内容
                    $("#orderUL").append(bookData);
                }); 
                $("#priceCount").html(priceCount);
                $("#numcount").html(numCount);
            }
        });
    }
    
    $(function () {
        GetCategory(@hId);
        GetCookbook(@hId,0);
        //GetOrderTempDetail(@uid,$("#orderid").val());
        $("#BtnNextOrder").click(function() {  
            var url = "@Url.Action("SetOrder","Yd")?hotelid=" + @hId+"&orderid="+$("#orderid").val();
            window.location = url;
            /* var tempurl = "@@Url.Action("UpdateOrder")?orderid="+orderid;
            $.ajax({
                url: tempurl,
                cache: false,
                type: 'POST',
                dataType: "json",  
                success: function (result) {  
                    if (result.Success == true) {
                     
                    }
                }
            }); */
        });
    });
</script>
<!--菜谱模版-->
<script type="text/template" id="CategoryTemp">   
	  	<dd id="{{Id}}"><a href="javascript:;">{{Name}}</a></dd>  
</script>

<!--    -->
<script type="text/template" id="CookbookTemp">   
	  <li>
					<div class="mainimg">
                        <a  class="example-image-link" data-lightbox="example-{{Id}}" href="@(RBS.Common.ConfigHelper.GetConfigString("hotelWeb")){{DishImages}}"><img src="@(RBS.Common.ConfigHelper.GetConfigString("hotelWeb")){{DishImages}}" alt="{{Name}}" class="example-image" onerror="javascript:this.src='@Url.Content("~/Content/noPic.png")';" /></a></div>
					<h3>{{Name}}</h3>
					<dl class="score">
						<dd class="money">￥<strong>{{Price}}/{{Unit}}</strong></dd>
					@*	<dd class="sales">销量：<b>215</b></dd>*@
					</dl>
					@*<p class="setu"  id="{{Id}}" name="{{Name}}" Price="{{Price}}"><a href="javascript:void(0);" >点菜</a></p>*@
				</li>
</script>
<!--已点菜谱模版-->
 <script type="text/template" id="OrderTemp">  
    <li> 
	    <div class="food-count-del"><a href="javascript:void(0);" data-change="-1" onclick="DeleteOrderTemp({{HCId}},1)">×</a></div>
	    <div class="food-count-name">{{CName}}</div>
	    <div class="food-count-price">{{Price}}</div>
	    <div class="food-count-reduce"  ><a href="javascript:void(0);" data-change="-1" onclick="AddOrder({{HCId}},'{{CName}}',{{Price}},-1);">-</a></div>
	    <div class="food-count-num">{{Num}}</div>
	    <div class="food-count-add"><a href="javascript:void(0);" data-change="1" onclick="AddOrder({{HCId}},'{{CName}}',{{Price}},1);">+</a></div>
    </li>
</script>
